var mongoose = require('mongoose');
var Products=require("../model/product");
module.exports={
    list(req,res,next){
        var page=(req.body.page)?req.body.page:1; 
        var limit=(req.body.limit)?req.body.limit:2;
        var queryCondition={};
        if(req.body.name&&req.body.name.trim().length>0){
            name=req.body.name;
            queryCondition={
                "name":new RegExp(name,"i")
            }
        }
        Products.paginate(queryCondition,{page:page,limit:limit},(error,results)=>{
            res.json(results);
        })
    },
    post(req,res,next){

        console.log(req.body);
        const products=new Products(req.body);
        products.save(function(err,data){
            if(err){
                console.log(err)
            }
            else{
                res.json(data);
            }
        })
    },
    put(req,res,next){
        const id=req.params.id;
        Products .findByIdAndUpdate(id,{$set:{name:req.body.name,
            price:req.body.price,
            count:req.body.count,
            img:req.body.img,
            intro:req.body.intro,
        }},{new:false}).then(result=>{
            res.json(result)
        })
    },
    delete(req,res,next){
        const id=req.params.id;
        Products.findByIdAndRemove(id,function(error,data){
            res.json("delete is ok!")
        })
    },
    deletemany(req,res,next){
        const id=req.body.id;
        Products.remove({"_id":{ $in:id}},function(error,data){
            if(error){
                console.log(error)
            }
            else{
                res.json("deletemany is ok!")
            } 
        })
    }
}
exports.list=function(req,res,next){
    var page=(req.body.page) ? req.body.page:1;
    var rows = (req.body.rows) ? req.body.rows : 10;

    var queryCondition = {};

    if(req.body.title && req.body.title.trim().length>0){
        title = req.body.title;
        queryCondition = {
            "title": new RegExp(title,'i')
        }
    }

    if(req.body.cateId && req.body.cateId.trim().length>0){
        id = req.body.cateId;
        var ids=[];
        Cate.findOne({_id:id},function(err,doc){
            if(doc){
                ids.push(doc.id);//加入该项
                doc.getChildren().then(function(docs){
                    for(var i=0;i<docs.length;i++){
                        ids.push(docs[i]._id);
                    } 
                    queryCondition = Object.assign(queryCondition,{           
                        cateId: {$in : ids}
                    })
                    //console.log('22',queryCondition)
                    News.paginate(queryCondition,{ page: +page, limit: +rows},function (err,result){
                       // console.log('222',queryCondition,rows);
                        result.rows = result.docs;
                        delete result.docs;
                        res.json(result);
                    })
                })               
            }        
        })       
    }else{
        News.paginate(queryCondition,{ page: +page, limit: +rows},function (err,result){
            result.rows = result.docs;
            delete result.docs;
            res.json(result);
        })
    }

}